package com.wc.fortnight_blue_bridge.Q241201.曲率驱动;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/12/2 16:09
 * @description
 * https://www.lanqiao.cn/problems/20052/learning/?contest_id=225
 */
public class Main {
    /**
     * 最短路问题
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, M = N * 2;
    static long INF = (long) 1e15;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static long[][] dist = new long[N][N];
    static boolean[][] st = new boolean[N][N];
    static PriorityQueue<long[]> heap = new PriorityQueue<>(Comparator.comparingLong(o -> o[0]));
    static int[] d = new int[N];
    static int[][] q = new int[N * N][2];
    static int n, m, k, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= m; i++){
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        m = sc.nextInt();
        for (int i = 1; i <= m; i++) d[i] = d[i - 1] ^ sc.nextInt();
        dijkstra();
        for (int i = 1; i <= n; i++) {
            long x = INF;
            for (int j = 0; j <= m; j++) x = Math.min(x, dist[i][j]);
            if (x == INF) x = -1;
            out.print(x + " ");
        }
        out.println();
        out.flush();
    }


    static void dijkstra() {
        for (int i = 1; i <= n; i++) Arrays.fill(dist[i], INF);
        Arrays.fill(dist[1], 0);
        heap.add(new long[]{0, 1, 0});
        while (!heap.isEmpty()) {
            long[] t = heap.poll();
            int x = (int) t[1], u = (int) t[2];
            if (st[x][u]) continue;
            st[x][u] = true;
            for (int i = h[x]; i > 0; i = ne[i]) {
                int j = e[i];
                for (int k = u; k <= m; k++) {
                    if (dist[j][k] > dist[x][u] + (w[i] ^ d[k])) {
                        dist[j][k] = dist[x][u] + (w[i] ^ d[k]);
                        heap.add(new long[]{dist[j][k], j, k});
                    }
                }
            }
        }
    }

    static void spfa() {

    }

    static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

